package com.cwj.mansysback.servlet.filter;

import com.cwj.mansysback.entity.LocalAccount;
import com.cwj.mansysback.servlet.impl.LoginServlet;
import com.cwj.mansysback.servlet.impl.Statement;
import com.cwj.mansysback.servlet.util.MyUtils;

import javax.servlet.*;
import java.io.IOException;

public class LockFilter implements Filter {

    public static int STATUE =4;

    public static final String IDCARDKEY = "idCard";
    public static final String NOWLOACC = "nowLoAcc";

    public static LocalAccount localAccount = null;

    @Override
    public void init(FilterConfig filterConfig) throws ServletException {

    }

    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {

        System.out.println("***************** LockServelt *************************");

        String idCard = request.getParameter(LockFilter.IDCARDKEY);

        Statement state = new Statement(LockFilter.STATUE,Statement.getMessage(LockFilter.STATUE));

        LocalAccount loAcc = null;

        if(idCard == null || (loAcc=LoginServlet.loadLoAcc(idCard)) == null){

//            System.out.println(idCard);

            MyUtils.writeResponse(response,state);

            System.out.println("被拦截idCard:"+idCard);

            return;
        }

//        request.setAttribute(LockFilter.NOWLOACC,loAcc);

        LockFilter.localAccount = loAcc;


        System.out.println("放行**************************");

        chain.doFilter(request, response);

    }

    @Override
    public void destroy() {

    }
}
